package com.shensoft.barcode.client.android.update;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.xmlpull.v1.XmlPullParser;

import android.util.Xml;

public class AndroidServiceXML {
	private static UpdataInfo info;
	/**
	 * 获取网络的xml
	 * 
	 * @return
	 * @throws Exception
	 */
	public static UpdataInfo getLastVideos(String path) throws Exception {		
		URL url = new URL(path);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setRequestMethod("GET");
		conn.setReadTimeout(1000 * 6);

		if (conn.getResponseCode() == 200) {
			InputStream inputStream = conn.getInputStream();
			return parseXML(inputStream);
		} else{
			info=null;
			return info;
		}
	}

	/**
	 * 解析XML数据
	 * 
	 * @param inputStream
	 * @return
	 * @throws Exception
	 */
	public static UpdataInfo parseXML(InputStream inputStream) throws Exception {
		XmlPullParser parser = Xml.newPullParser();
		parser.setInput(inputStream, "utf-8");// 设置解析的数据源
		int type = parser.getEventType();
		info = new UpdataInfo();// 实体
		while (type != XmlPullParser.END_DOCUMENT) {
			switch (type) {
			case XmlPullParser.START_TAG:
				if ("version".equals(parser.getName())) {
					info.setVersion(parser.nextText()); // 获取版本号
				} else if ("android_adp".equals(parser.getName())) {
					info.setAndroid_adp(parser.nextText()); // 获取版本号
				} else if ("url".equals(parser.getName())) {
					info.setUrl(parser.nextText()); // 获取要升级的APK文件
				} else if ("description".equals(parser.getName())) {
					info.setDescription(parser.nextText()); // 获取该文件的信息
				}
				break;
			}
			type = parser.next();
		}
		return info;
	}
}
